package com.cfy.ssm.controller;


import com.cfy.ssm.controller.LoginInfo;
import com.dubbo.service.api.FooService;
import com.dubbo.service.api.WeatherService;
import com.dubbo.service.api.mainpk.TodayWeather;
import com.dubbo.service.api.mainpk.User;
import org.springframework.beans.factory.support.ManagedMap;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.web.servlet.ModelAndView;
//import org.springframework.web.servlet.mvc.Controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by: zs
 * Date-Time: 2017/8/9 19:05
 */

@Controller
public class WelcomeController{
    @RequestMapping(value = "/login")  //通过spring注解来映射方法和url
    public String login(){
        return "login";
    }

    @RequestMapping(value = "/loginCheck")
    public ModelAndView loginCheck(HttpServletRequest httpServletRequest,LoginInfo loginInfo) throws  Exception{
        ApplicationContext ac = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
        ((ClassPathXmlApplicationContext) ac).start();//启动消费者测试项目

        System.out.print("SpringMVC Consumer Start......");
        FooService Us = (FooService)ac.getBean("mvcService");
        System.out.print("consumer"+'\n');
        User user_mvc = Us.doSomeBusinessStuff(loginInfo.getUserName());

        if(user_mvc.getId().equals(loginInfo.getUserName()) && user_mvc.getName().equals(loginInfo.getPassword()))
        {
            //账号密码正确就显示天气信息
            ApplicationContext acs = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
            ((ClassPathXmlApplicationContext) acs).start();//启动消费者测试项目
            WeatherService Ws = (WeatherService)acs.getBean("mvcWeatherService");
            List<TodayWeather> tw = Ws.getAllWeather();
            System.out.println(tw);

            ModelAndView mav = new ModelAndView();
            Map<String,List> weatherToJSP = new HashMap<String, List>();
            weatherToJSP.put("now",tw);
            mav.addObject("map",weatherToJSP);
            mav.setViewName("welcome");

            return mav;
        }
        else
        {
            return new ModelAndView("error");
        }
    }
}

